/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucuenca.ec.nosql.controller;

import edu.ucuenca.ec.nosql.conexion.Neo4jEngine;
import edu.ucuenca.ec.nosql.modelos.Persona;
import edu.ucuenca.ec.nosql.security.CarritoSession;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author mivkys
 */
@ManagedBean(name = "amigosController")
@SessionScoped
public class AmigosController implements Serializable {

    private Persona selected;
    private List<Persona> amigos;

    public AmigosController() {

    }

    @PostConstruct
    public void init() {///para actualizar
        System.out.println("posconstruc amigoscontroller");
        amigos = new ArrayList<>();
        if (CarritoSession.getUsuario() != null) {
            amigos = CarritoSession.getAmigos();
        }
    }

    public void flush() {
        if (CarritoSession.getUsuario() != null) {
            amigos = CarritoSession.getAmigos();
        }else{
            amigos = new ArrayList<>();
            CarritoSession.setAmigos(amigos);
        }
    }

    public void actualizar() {
        if (CarritoSession.getUsuario() != null) {
            Neo4jEngine.conexionFactory();
            amigos = Neo4jEngine.matchAmigos(CarritoSession.getUsuario());
            CarritoSession.setAmigos(amigos);
        }
    }

    /**
     * @return the selected
     */
    public Persona getSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    public void setSelected(Persona selected) {
        this.selected = selected;
    }

    /**
     * @return the amigos
     */
    public List<Persona> getAmigos() {
        return amigos;
    }

    /**
     * @param amigos the amigos to set
     */
    public void setAmigos(List<Persona> amigos) {
        this.amigos = amigos;
    }

}
